package com.isunland.managesystem.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import com.isunland.managesystem.R;

/**
 * 是否支付
 *
 * @author wu
 */
public class DeliveryIfPayDialogFragment extends DialogFragment {
  protected static final String EXTRA_IF_PAY = "com.isunland.managesystem.ui.EXTRA_IF_PAY";
  public static final String EXTRA_LOGISTICS = "com.isunland.managesystem.ui.EXTRA_LOGISTICS";
  public static final String EXTRA_IFPAY = "com.isunland.managesystem.ui.EXTRA_IFPAY";
  public static final String REQUEST_TYPE = "com.isunland.managesystem.ui.REQUEST_TYPE";
  protected static final String EXTRA_THIRD_LOGISTICS =
      "com.isunland.managesystem.ui.EXTRA_THIRD_LOGISTICS";
  protected static final String EXTRA_PAY = "com.isunland.managesystem.ui.EXTRA_PAY";
  private int[] choiceIds = new int[] { R.string.isTrue, R.string.isFalse };
  private String[] choiceStrings = new String[choiceIds.length];

  private String mType;

  @Override public void onCreate(Bundle savedInstanceState) {
    mType = getArguments().getString(REQUEST_TYPE);
    super.onCreate(savedInstanceState);
  }

  @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(getActivity()).setSingleChoiceItems(getChoiceName(), 0,
        new OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {

            String isPay;
            switch (which) {
              case 0:
                isPay = DeliveryListQueryFragment.VALUE_TRUE;
                break;
              case 1:
                isPay = DeliveryListQueryFragment.VALUE_FALSE;
                break;
              default:
                isPay = "";
                break;
            }

            if (getTargetFragment() == null) return;
            Intent i = new Intent();
            if (EXTRA_LOGISTICS.equalsIgnoreCase(mType)) {
              i.putExtra(EXTRA_THIRD_LOGISTICS, isPay);
            }

            if (EXTRA_IFPAY.equalsIgnoreCase(mType)) {
              i.putExtra(EXTRA_PAY, isPay);
            }

            getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
            dialog.dismiss();
          }
        }).create();
  }

  private String[] getChoiceName() {
    for (int i = 0; i < choiceIds.length; i++) {
      choiceStrings[i] = getResources().getString(choiceIds[i]);
    }
    return choiceStrings;
  }
}
